package com.amap.poi.controller;

import cn.hutool.json.JSONUtil;
import com.amap.poi.common.exception.Asserts;
import com.amap.poi.common.result.CodeMsg;
import com.amap.poi.common.result.Result;
import com.amap.poi.model.dto.PoiDataDTO;
import com.amap.poi.model.req.TextReq;
import com.amap.poi.service.IAmapSearchService;
import com.amap.poi.service.ICityService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

/**
 * 高德地图搜索 - 控制器
 *
 * @author L
 */
@Controller
@Slf4j
public class AmapSearchController {

    @Autowired
    private IAmapSearchService amapSearchService;
    @Autowired
    private ICityService cityService;


    /**
     * 省份城市搜索
     *
     * @param keyword
     * @return
     */
    @PostMapping(value = "/amap/search/queryCity")
    @ResponseBody
    public Result<Map<String, Object>> queryCity(@RequestParam(name = "keyword") String keyword) {
        log.info("【省份城市搜索接口】｜ 请求参数={}", keyword);
        // 校验
        if (StringUtils.isBlank(keyword)) {
            Asserts.fail(CodeMsg.BIND_ERROR.getCode(), "请求参数不能为空");
        }
        // 查询
        Map<String, Object> map = cityService.queryByCondition(keyword);
        return Result.success(map);
    }

    /**
     * 关键词搜索接口
     *
     * @param req
     * @return
     */
    @PostMapping(value = "/amap/search/queryByText")
    @ResponseBody
    public Result<List<PoiDataDTO>> queryByText(@RequestBody TextReq req) {
        log.info("【关键词搜索接口】｜ 请求参数={}", JSONUtil.toJsonStr(req));
        // 校验
        if (StringUtils.isBlank(req.getKeywords())) {
            Asserts.fail(CodeMsg.BIND_ERROR.getCode(), "关键词不能为空");
        }
        // 查询
        List<PoiDataDTO> list = amapSearchService.queryByText(req);
        return Result.success(list);
    }

}
